package com.ujoku.controller.admin;

import com.labillusion.core.platform.cdn.upyum.UpYun;
import com.labillusion.core.platform.web.rest.RESTController;
import com.rits.cloning.Cloner;
import com.ujoku.domain.TopBanner;
import com.ujoku.domain.Winery;
import com.ujoku.service.TopBannerService;
import com.ujoku.service.WineryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Greg.Chen on 14-11-12.
 */
@RestController
public class TopBannerMngController extends RESTController {

    @Autowired
    private TopBannerService service;

    @Autowired
    private UpYun upyun;

    @Value("#{configProperties['settings.topBanner.upload']}")
    private String uploadPath;

    @Value("#{configProperties['settings.cdn.topBanner.upload']}")
    private String cdnUploadPath;

    @RequestMapping(value = "/uj-admin/topBanner/list", method = RequestMethod.GET)
    @ResponseBody
    public List<TopBanner> list(){

        List<TopBanner> topBannerList = service.selectList();

        return topBannerList;
    }

    @RequestMapping(value = "/uj-admin/topBanner/{id}", method = RequestMethod.GET)
    @ResponseBody
    public TopBanner getById(@PathVariable String id){
        return (TopBanner)service.selectById(id);
    }

    @RequestMapping(value = "/uj-admin/topBanner", method = RequestMethod.POST)
    @ResponseBody
    public TopBanner add(@RequestBody TopBanner topBanner){
        service.insert(topBanner);
        return topBanner;
    }

    @RequestMapping(value = "/uj-admin/topBanner", method = RequestMethod.PUT)
    @ResponseBody
    public TopBanner update(@RequestBody TopBanner topBanner){
        service.update(topBanner);
        return topBanner;
    }

    @RequestMapping(value = "/uj-admin/topBanner/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable int id, HttpServletRequest request) throws IOException {
        service.deleteById(id);

        String dir = request.getRealPath(uploadPath);
        String path = dir+"/"+ id + ".jpg";


        File  localFile = new File(path);
        if(localFile.exists()) {
            upyun.setContentMD5(UpYun.md5(localFile));
            upyun.deleteFile(cdnUploadPath + localFile.getName());
        }
        return "successfully";
    }

    @RequestMapping(value="/uj-admin/topBanner/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(  @RequestParam MultipartFile file,
                           @RequestParam String id, HttpServletRequest request) throws IOException {

        TopBanner topBanner =  (TopBanner)service.selectById(id);
        //更新时间戳
        service.update(topBanner);

        String dir = request.getRealPath(uploadPath);
        File folder = new File(dir);
        if(!folder.exists()){
            folder.mkdirs();
        }

        String originalPath = dir+"/"+ topBanner.getId() + ".jpg";
        File originalFile = new File(originalPath);
        //将上传文件写入到指定文件出
        file.transferTo(originalFile);

        String path = dir+"/"+ topBanner.getId() + ".jpg";

        File  localFile = new File(path);
        upyun.setContentMD5(UpYun.md5(localFile));
        upyun.writeFile(cdnUploadPath + localFile.getName(), localFile, true);

        return "Successfully";
    }
}
